import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ManagePatientsFrame extends JPanel {
	// MAIN PANELS
	private JTabbedPane manageTabs;
	private JPanel pPanel, pHeader, pBody, pPersonal, pMedical, pPatientLog;
	private JList pList;

	// HEADER OBJECTS
	private JLabel pHName;
	private JComboBox <String> pSelect;
	private JButton pDelete;

	// PERSONAL OBJECTS
	private JPanel ppNames, ppAddress, ppLocation, ppVital;
	private JTextField fName, lName, city, state, zip, ssn, birthdate, insurance, insuranceID;
	private JComboBox <String> ppTitle, ppSex;
	private JTextArea address;
	private JButton pSavePersonal;

	// MEDICAL OBJECTS
	private JPanel pmEntry, pmEntryHead, pmEntryBMI, pmEntryPrescriptions, pmEntryBlood, pmEntryNotes;
	private JList pmList;
	private JLabel pmEntryDate, pmEntryAuthor;
	private JTextField weight, height, bmi, systolic, diastolic, bloodSugar;
	private JTextArea prescriptions, notes;
	private JButton pSaveMedical;

	// PATIENT LOG OBJECTS
	private JPanel pplComments, pplCommentsBox, pplGraph;
	private JList pplLog;
	private JLabel pplCommentsDate;
	private JTextArea comments;

	//==== CONSTRUCTOR ====//
	public ManagePatientsFrame(int ID, int userType)
	{
		//---- OBJECT INITIALIZATION ----//
		String[] pPanels = {"Personal", "Medical", "Patient Log"};
		String[] titles = {"Mr.", "Mrs.", "Ms.", "Miss", "Master"};
		String[] sexes = {"Male", "Female", "Other"};
		manageTabs = new JTabbedPane();
		pPanel = new JPanel();
		pHeader = new JPanel();
		pBody = new JPanel();
		pPersonal = new JPanel();
		pMedical = new JPanel();
		pPatientLog = new JPanel();
		// check user type/id to determine list population
		pList = new JList();
		pSelect =  new JComboBox<String>(pPanels);
		pDelete = new JButton("Delete");

		//---- PATIENT PANEL ----//
		//---- Header ----//
		pSelect.setSelectedIndex(0);
		pHName = new JLabel("[" + ID + "] Jane Doe");
		pHeader.add(pHName);
		pHeader.add(pSelect);
		if (userType == 3)
			pHeader.add(pDelete);

		//---- Personal ----//
		pPersonal.setLayout(new BoxLayout(pPersonal, BoxLayout.PAGE_AXIS));

		// Initialization
		ppNames = new JPanel();
		ppAddress = new JPanel();
		ppLocation = new JPanel();
		ppVital = new JPanel();
		ppNames.setLayout(new GridLayout(2, 4));
		ppAddress.setLayout(new BorderLayout());
		ppLocation.setLayout(new GridLayout(2, 4));
		ppVital.setLayout(new GridLayout(2, 4));

		fName = new JTextField("");
		lName = new JTextField("");
		city = new JTextField("");
		state = new JTextField("");
		zip = new JTextField("");
		ssn = new JTextField("");
		birthdate = new JTextField("");
		insurance = new JTextField("");
		insuranceID = new JTextField("");
		address = new JTextArea("");
		ppTitle = new JComboBox<String>(titles);
		ppTitle.setSelectedIndex(0);
		ppSex = new JComboBox<String>(sexes);
		ppSex.setSelectedIndex(0);
		pSavePersonal = new JButton("Save");

		// Insertion
		ppNames.add(new JLabel("First Name: "));
		ppNames.add(fName);
		ppNames.add(new JLabel("Title: "));
		ppNames.add(ppTitle);
		ppNames.add(new JLabel("Last Name: "));
		ppNames.add(lName);
		ppNames.add(new JLabel("Sex: "));
		ppNames.add(ppSex);

		ppAddress.add(new JLabel("Address: "), BorderLayout.WEST);
		ppAddress.add(address, BorderLayout.CENTER);

		ppLocation.add(new JLabel("City: "));
		ppLocation.add(city);
		ppLocation.add(new JLabel("State: "));
		ppLocation.add(state);
		ppLocation.add(new JLabel("Zip: "));
		ppLocation.add(zip);
		ppLocation.add(new JLabel(""));		// spacing
		ppLocation.add(new JLabel(""));		// spacing

		ppVital.add(new JLabel("SSN: "));
		ppVital.add(ssn);
		ppVital.add(new JLabel("Birth: "));
		ppVital.add(birthdate);
		ppVital.add(new JLabel("Insurance Company: "));
		ppVital.add(insurance);
		ppVital.add(new JLabel("Insurance ID: "));
		ppVital.add(insuranceID);

		// Combination
		pPersonal.add(ppNames);
		pPersonal.add(ppAddress);
		pPersonal.add(ppLocation);
		pPersonal.add(ppVital);
		pPersonal.add(pSavePersonal);

		//---- Medical ----//
		pMedical.setLayout(new GridLayout(2, 1));

		// Initialization
		pmEntry = new JPanel();
		pmEntryHead = new JPanel();
		pmEntryBMI = new JPanel();
		pmEntryPrescriptions = new JPanel();
		pmEntryBlood = new JPanel();
		pmEntryNotes = new JPanel();
		pmEntry.setLayout(new BoxLayout(pmEntry, BoxLayout.PAGE_AXIS));
		pmEntryHead.setLayout(new GridLayout(1, 4));
		pmEntryBMI.setLayout(new GridLayout(1, 6));
		pmEntryPrescriptions.setLayout(new BorderLayout());
		pmEntryBlood.setLayout(new GridLayout(2, 5));
		pmEntryNotes.setLayout(new BorderLayout());

		pmList = new JList(); // populate jlist
		pmEntryDate = new JLabel("12/22/12");
		pmEntryAuthor = new JLabel("Added by [S0001] John Doe");
		weight = new JTextField("");
		height = new JTextField("");
		bmi = new JTextField("");
		systolic = new JTextField("");
		diastolic = new JTextField("");
		bloodSugar = new JTextField("");
		prescriptions = new JTextArea("");
		notes = new JTextArea("");
		pSaveMedical = new JButton("Save");

		// Insertion
		pmEntryHead.add(pmEntryDate);
		pmEntryHead.add(pmEntryAuthor);

		pmEntryBMI.add(new JLabel("Weight (kg): "));
		pmEntryBMI.add(weight);
		pmEntryBMI.add(new JLabel("Height (cm): "));
		pmEntryBMI.add(height);
		pmEntryBMI.add(new JLabel("BMI: "));
		pmEntryBMI.add(bmi);

		pmEntryPrescriptions.add(new JLabel("Prescrpitons: "), BorderLayout.WEST);
		pmEntryPrescriptions.add(prescriptions, BorderLayout.CENTER);

		pmEntryBlood.add(new JLabel("Blood Pressure: "));
		pmEntryBlood.add(new JLabel("Systolic: "));
		pmEntryBlood.add(systolic);
		pmEntryBlood.add(new JLabel("Diastolic: "));
		pmEntryBlood.add(diastolic);
		pmEntryBlood.add(new JLabel("Blood Sugar: "));
		pmEntryBlood.add(bloodSugar);
		pmEntryBlood.add(new JLabel("mmol/l"));
		pmEntryBlood.add(new JLabel(""));	// spacing
		pmEntryBlood.add(new JLabel(""));	// spacing

		pmEntryNotes.add(new JLabel("Doctor's Notes: "), BorderLayout.NORTH);
		pmEntryNotes.add(notes, BorderLayout.CENTER);

		// Combination
		pmEntry.add(pmEntryHead);
		pmEntry.add(pmEntryBMI);
		pmEntry.add(pmEntryPrescriptions);
		pmEntry.add(pmEntryBlood);
		pmEntry.add(pmEntryNotes);
		pmEntry.add(pSaveMedical);

		pMedical.add(pmList);
		pmList.setBorder(BorderFactory.createLineBorder(Color.black));
		pMedical.add(pmEntry);

		//---- Patient Log ----//
		pPatientLog.setLayout(new GridLayout(3, 1));

		// Initialization
		pplComments = new JPanel();
		pplCommentsBox = new JPanel();
		pplGraph = new JPanel();
		pplComments.setLayout(new BorderLayout());
		pplCommentsBox.setLayout(new BorderLayout());

		pplLog = new JList();	// populate list
		pplCommentsDate = new JLabel("02/09/13");
		comments = new JTextArea("");

		// Insertion
		pplComments.add(pplCommentsDate, BorderLayout.NORTH);
		pplCommentsBox.add(new JLabel("Comments:"), BorderLayout.NORTH);
		pplCommentsBox.add(comments, BorderLayout.CENTER);
		pplComments.add(pplCommentsBox, BorderLayout.CENTER);

		// Combination
		pPatientLog.add(pplLog);
		pplLog.setBorder(BorderFactory.createLineBorder(Color.black));
		pPatientLog.add(pplComments);
		pPatientLog.add(pplGraph);

		//---- MAIN LAYOUT ----//
		pBody.setLayout(new CardLayout());
		pBody.add(pPersonal, "Personal");
		pBody.add(pMedical, "Medical");
		pBody.add(pPatientLog, "Patient Log");
		pBody.setBorder(BorderFactory.createLineBorder(Color.black));
		setLayout(new BorderLayout());
		add(pList, BorderLayout.WEST);
		pPanel.setLayout(new BorderLayout());
		pPanel.add(pHeader, BorderLayout.NORTH);
		pPanel.add(pBody, BorderLayout.CENTER);
		add(pPanel, BorderLayout.CENTER);

		// Button Listeners
		// List Listeners
		// Combo Box Listeners
		ComboListener combo = new ComboListener();
		pSelect.addItemListener(combo);
	}

	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			Object action = event.getSource();
		}
	}

	private class ComboListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent event)
		{
			Object action = event.getSource();
			if (action == pSelect)
			{
				CardLayout selection = (CardLayout) (pBody.getLayout());
				selection.show(pBody, (String)event.getItem());
			}
		}
	}
}